﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WAD.WEB.AppCode;
using WAD.Model;
using WAD.DAL;
using WAD.DAL.Admin;

namespace WAD.WEB.Admin.Ajax
{
    /// <summary>
    /// Article 的摘要说明
    /// </summary>
    public class Article : AdminAjax
    {
        public override void AjaxMethodDispatch(String identity)
        {
            switch (identity)
            {
                case "getlist": this.GetList(); break;
                case "getchanne": this.Getchanne(); break;
                case "updatechanne": this.UpdateChanne(); break;
                case "detail_filter": this.Detail_Filter(); break;
                case "check": this.Check(); break;
                case "delete": this.Delete(); break;
            }
        }
        /// <summary>
        /// 获取文章列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void GetList()
        {
            String keyWord = Parse.String("key_word");//搜索的关键词
            int channelId = Parse.Int("channel_id", 0);//获取频道
            int authorId = Parse.Int("author_id", 0);//作者id
            int pageIndex = Parse.Int("page_index", 1);//当前页
            MArticle m = new MArticle() { channel_id = channelId, key_word = keyWord, user_id = authorId, title = Parse.String("title"), state = Parse.Int("state", 0) };

            PageData pg = article.GetArticlelist(pageIndex, 20, m, "id,user_id,title,logo,click,state,[date]");
            if (pg.itemCount == 0)
            {
                DataEmpty();
            }
            else
            {
                Success(pg);
            }
        }

        /// <summary>
        /// 获取频道列表
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void Getchanne()
        {
            int pageIndex = Parse.Int("page_index", 1);//当前页

            PageData pg = DaoChannel.GetList(pageIndex, 20);

            if (pg.itemCount == 0)
            {
                DataEmpty();
            }
            else
            {
                Success(pg);
            }
        }


        /// <summary>
        /// 更改文章频道
        /// </summary>
        [QD.Web.Ajax.AjaxMethod]
        public void UpdateChanne()
        {
            string id = Parse.String("id", "");
            int channeid = Parse.Int("channeid", 1);


            if (!article.UpdateArtcha(id, channeid))
            {
                DataEmpty();
            }
            else
            {
                Success();
            }
        }

        [QD.Web.Ajax.AjaxMethod]
        public void Detail_Filter()
        {
            int id = Parse.Int("id", 0);
            object nextId = null;
            Dictionary<string, object> model = DaoArticle.Get("article", "state!=0 and id=" + id, "id,title,click,logo,date,content");
            if (model != null)
            {
                Dictionary<string, object> next = DaoArticle.Get("article", "state!=0 and is_pass=0 and id!=" + id, "id");
                if (next != null)
                    nextId = next["id"];
                model.Add("next_id", nextId);
            }
            JsonResult(0, "", model);
        }

        [QD.Web.Ajax.AjaxMethod]
        public void Check()
        {
            int id = Parse.Int("id", 0);
            int state = Parse.Int("state", 0);
            DaoArticle.ExecuteNonQuery(string.Format("update article set state={0} where id={1}", state, id));
            JsonResult(0, "");
        }

        [QD.Web.Ajax.AjaxMethod]
        public void Delete()
        {
            int id = Parse.Int("id", 0);
            DaoArticle.ExecuteNonQuery(string.Format("delete article where id={0}", id));
            JsonResult(0, "");
        }
    }
}